package com.dahouzi.config;

import javax.sql.DataSource;

import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;

@Configuration
@EnableTransactionManagement
@MapperScan("com.dahouzi.dao.mapper")
public class SessionFactoryConfig  {
	private static String MYBATIS_CONFIG = "mybatis-config.xml";// mybatis 配置路径

	@Autowired
	private DataSource dataSource;
	@Autowired
	private MybatisProperties properties;
	@Autowired
	private ResourceLoader resourceLoader = new DefaultResourceLoader();
	@Autowired(required = false)
	private Interceptor[] interceptors;
	@Autowired(required = false)
	private DatabaseIdProvider databaseIdProvider;

	/**
	 * 这里全部使用mybatis-autoconfigure 已经自动加载的资源。不手动指定 配置文件和mybatis-boot的配置文件同步
	 *
	 * @return
	 */
	@Bean
	public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() {
		MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
		mybatisPlus.setDataSource(dataSource);
		//mybatisPlus.setVfs(SpringBootVFS.class);
		mybatisPlus.setConfigLocation(new ClassPathResource(MYBATIS_CONFIG));// 设置xml配置mybatis
		return mybatisPlus;
	}

	@Bean
	public PaginationInterceptor paginationInterceptor() {
		PaginationInterceptor page = new PaginationInterceptor();
		System.out.println("************设置分页插件*****************");
		page.setDialectType("mysql");
		return page;
	}





}